package com.lzx.iteam;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.*;
import android.widget.*;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetMsgHttpReceiver;
import com.lzx.iteam.net.GetUserVipMsg;
import com.lzx.iteam.net.LocalLogin;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


/**
 * Created by Megan on 16/8/5.
 */
public class AccountUpgradeActivity extends MyBaseActivity implements View.OnClickListener {

    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.tv_account_upgrade_state1)
    TextView mTvState300;
    @FindViewById(id = R.id.ll_account_upgrade_layout200)
    private LinearLayout mLayout200;
    @FindViewById(id = R.id.ll_account_upgrade_layout300)
    private LinearLayout mLayout300;
    @FindViewById(id = R.id.btn_account_upgrade)
    private Button mBtnUpgrade;

    //private PopupWindow mPopupWindow;
    private final int MSG_UPGRADE_ACCOUNT = 1001;
    private final int MSG_GET_VIP_MSG = 1002;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPGRADE_ACCOUNT:
                    if (msg.arg1 == 0) {
                        Toast.makeText(mActivity, "升级成功",
                                Toast.LENGTH_LONG).show();
                        getVipMsg();
                    } else if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                        Toast.makeText(mActivity, R.string.active_failure_no_network,
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(mActivity, (String) msg.obj,
                                Toast.LENGTH_LONG).show();
                    }
                    break;
                case MSG_GET_VIP_MSG:
                    if (msg.arg1 == 0) {
                        int level = (Integer) msg.obj;
                        if (level == 0) {
                            mLayout200.setVisibility(View.VISIBLE);
                            mLayout300.setVisibility(View.VISIBLE);
                            mTvState300.setVisibility(View.GONE);
                            mBtnUpgrade.setEnabled(true);
                        } else if (level == 1) {//300用户
                            mLayout200.setVisibility(View.GONE);
                            mLayout300.setVisibility(View.VISIBLE);
                            mTvState300.setVisibility(View.VISIBLE);
                            mBtnUpgrade.setEnabled(false);
                        }
                    } else if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                        Toast.makeText(mActivity, R.string.active_failure_no_network,
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(mActivity, (String) msg.obj,
                                Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.account_upgrade_layout);
        getVipMsg();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.btn_account_upgrade:
                showUpgradeWindow();
                break;
        }
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mBtnUpgrade.setOnClickListener(this);

    }

    @Override
    public void initData() {
        mTvTitle.setText("账户升级");
        mTvRight.setVisibility(View.GONE);
    }

/*
    public void showUpgradeWindow() {
        View mainView = getLayoutInflater().inflate(R.layout.popwindow_accound_upgrade, null);
        final EditText edit = (EditText) mainView.findViewById(R.id.et_accound_upgrade_edit);
        ImageView upgrade = (ImageView) mainView.findViewById(R.id.iv_accound_upgrade_btn);
        TextView close = (TextView) mainView.findViewById(R.id.tv_accound_upgrade_close);
        mPopupWindow = new PopupWindow(this);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        mPopupWindow.setContentView(mainView);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setAnimationStyle(R.style.bottomStyle);

        mPopupWindow.showAtLocation(mainView, Gravity.BOTTOM, 0, 0);
        mPopupWindow.update();
        close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        upgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = edit.getText().toString().trim();
                upgradeAccount(code);
                Toast.makeText(AccountUpgradeActivity.this, "code=" + code, Toast.LENGTH_LONG).show();
            }
        });
        mainView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mPopupWindow.dismiss();
                return false;
            }
        });
    }
*/

    private void showUpgradeWindow() {

        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setView(new EditText(this));
        dialog.show();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.dimAmount = 0.3f; // TODO 设置黑暗度  0.0f完全不暗，1.0f全暗
//        lp.alpha = 0.5f; // TODO 设置透明度    0.0f完全透明，1.0完全不透明
        window.setAttributes(lp);
        // 设置布局
        window.setContentView(R.layout.popwindow_accound_upgrade);
        // 设置宽高
        window.setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置弹出的动画效果
        window.setWindowAnimations(R.style.bottomStyle);
        final EditText edit = (EditText) window.findViewById(R.id.et_accound_upgrade_edit);
        ImageView upgrade = (ImageView) window.findViewById(R.id.iv_accound_upgrade_btn);
        TextView close = (TextView) window.findViewById(R.id.tv_accound_upgrade_close);

        close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        upgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = edit.getText().toString().trim();
               if (Pattern.matches("[a-z0-9]{12}",code)) {
              //  if (Pattern.matches("[a-z0-9]{8}\\d{4}",code)) {
                   upgradeAccount(code);
               } else {
                   Toast.makeText(AccountUpgradeActivity.this, "升级码格式错误", Toast.LENGTH_LONG).show();
                }
            }
        });

        LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_cancel);
        cancelLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                dialog.cancel();
                return false;
            }
        });
    }


    private void upgradeAccount(String code) {
        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        mList.add(new BasicNameValuePair("code", code));
        GetMsgHttpReceiver message = new GetMsgHttpReceiver(mHandler.obtainMessage(MSG_UPGRADE_ACCOUNT));
        message.mApi = AsynHttpClient.API_USER_UPGRADE;
        message.mParams = mList;
        AsynHttpClient.getInstance(this).execUserHttp(message);

    }


    private void getVipMsg() {
        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        GetUserVipMsg message = new GetUserVipMsg(mHandler.obtainMessage(MSG_GET_VIP_MSG));
        message.mApi = AsynHttpClient.API_USER_VIP;
        message.mParams = mList;
        AsynHttpClient.getInstance(this).execUserHttp(message);

    }


}
